import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/commons/redirect')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/commons/login'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/commons/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/auto/auto-case-product'
    // redirect: '/dashboard',
    // children: [{
    //   path: 'dashboard',
    //   name: 'Dashboard',
    //   component: () => import('@/views/commons/dashboard'),
    //   meta: { title: '测试看板', icon: 'dashboard' }
    // }]
  },

  {
    // 暂时不做权限，先隐藏这个模块
    hidden: true,
    path: '/system',
    component: Layout,
    redirect: '/system/system-user',
    name: 'system-conf',
    meta: { title: '系统配置', icon: 'system' },
    children: [
      {
        path: 'system-user',
        name: 'SystemUser',
        component: () => import('@/views/modules/system/user'),
        meta: { title: '用户管理', icon: 'user' }
      },
      {
        path: 'system-role',
        name: 'SystemRole',
        component: () => import('@/views/modules/system/role'),
        meta: { title: '角色管理', icon: 'role' }
      },
      {
        path: 'system-permission',
        name: 'SystemPermission',
        component: () => import('@/views/modules/system/permission'),
        meta: { title: '权限管理', icon: 'permissions' }
      }
    ]
  },

  {
    path: '/auto',
    component: Layout,
    redirect: '/auto/auto-index',
    name: 'auto-test',
    meta: { title: '自动化测试', icon: 'flash_auto' },
    children: [
      {
        path: 'auto-index',
        name: 'AutoIndex',
        hidden: true,
        component: () => import('@/views/modules/auto/index'),
        meta: { title: '自动化看板', icon: 'icon-indexnor1' }
      },
      {
        path: 'auto-case-base',
        name: 'AutoCaseBase',
        component: () => import('@/views/modules/auto/case-base'),
        meta: { title: '基线用例', icon: 'skill' }
      },
      {
        path: 'auto-case-product',
        name: 'AutoCaseProduct',
        component: () => import('@/views/modules/auto/case-product'),
        meta: { title: '产品列表', icon: 'Product' }
      },
      {
        path: 'auto-case-demand/:productId',
        name: 'AutoCaseDemand',
        hidden: true,
        component: () => import('@/views/modules/auto/case-demand'),
        meta: { title: '需求列表', icon: 'Product' }
      },
      {
        path: 'auto-demand-case-list/:demandId',
        name: 'AutoDemandCaseList',
        hidden: true,
        component: () => import('@/views/modules/auto/case-list'),
        meta: { title: '需求-用例列表', icon: 'Product' }
      },
      {
        path: 'auto-task-case-list/:taskId',
        name: 'AutoTaskCaseList',
        hidden: true,
        component: () => import('@/views/modules/auto/case-list'),
        meta: { title: '任务-用例列表', icon: 'Product' }
      },
      {
        path: 'auto-module-case-list/:moduleId',
        name: 'AutoModuleCaseList',
        hidden: true,
        component: () => import('@/views/modules/auto/case-list'),
        meta: { title: '模块-用例列表', icon: 'Product' }
      },
      {
        path: 'auto-case-script/:caseId',
        name: 'AutoCaseScript',
        hidden: true,
        component: () => import('@/views/modules/auto/case-script'),
        meta: { title: '脚本编辑', icon: 'script' }
      },
      {
        path: 'auto-case-task',
        name: 'AutoCaseTask',
        component: () => import('@/views/modules/auto/case-task'),
        meta: { title: '任务管理', icon: 'tasks' }
      },
      {
        path: 'auto-case-task-report/:taskId',
        name: 'AutoCaseTaskReport',
        hidden: true,
        component: () => import('@/views/modules/auto/case-task-report'),
        meta: { title: '任务报告', icon: 'tasks' }
      },
      {
        path: 'auto-case-log',
        name: 'AutoCaseLog',
        component: () => import('@/views/modules/auto/case-log'),
        meta: { title: '日志管理', icon: 'log' }
      },
      {
        path: 'auto-case-log-detail/:logId',
        name: 'AutoCaseLogDetail',
        hidden: true,
        component: () => import('@/views/modules/auto/case-log-detail'),
        meta: { title: '日志详情', icon: 'log' }
      }
    ]
  },

  {
    path: '/perf',
    component: Layout,
    redirect: '/perf/perf-index',
    name: 'perf-test',
    meta: { title: '性能测试', icon: 'et-performance' },
    children: [
      {
        path: 'perf-index',
        name: 'PerfIndex',
        component: () => import('@/views/modules/perf/index'),
        meta: { title: '性能看板', icon: 'icon-indexnor1' }
      },
      {
        path: 'perf-task',
        name: 'PerfTask',
        component: () => import('@/views/modules/perf/task'),
        meta: { title: '任务管理', icon: 'tasks' }
      },
      {
        path: 'perf-task-detail/:taskId',
        name: 'PerfTaskDetail',
        hidden: true,
        component: () => import('@/views/modules/perf/task-detail'),
        meta: { title: '任务详情', icon: 'tasks' }
      },
      {
        path: 'perf-report',
        name: 'PerfReport',
        component: () => import('@/views/modules/perf/report'),
        meta: { title: '报告管理', icon: 'report' }
      },
      {
        path: 'perf-report-detail/:reportId',
        name: 'PerfReportDetail',
        hidden: true,
        component: () => import('@/views/modules/perf/report-detail'),
        meta: { title: '报告详情', icon: 'report' }
      },
      {
        path: 'perf-script',
        name: 'PerfScript',
        component: () => import('@/views/modules/perf/script'),
        meta: { title: '脚本管理', icon: 'script' }
      },
      {
        path: 'perf-resource',
        name: 'PerfResource',
        component: () => import('@/views/modules/perf/resource'),
        meta: { title: '资源文件', icon: 'csv' }
      },
      {
        path: 'perf-agent',
        name: 'PerfAgent',
        component: () => import('@/views/modules/perf/agent'),
        meta: { title: '执行机管理', icon: 'server' }
      }
    ]
  }
]

/**
 * 根据用户权限动态加载的路由 TODO
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://gitee.com/sunshine_2016/AutoTestProjects',
        meta: { title: 'AutoTestProjects', icon: 'bug' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
